
	import Util.DrawTree;
	import Util.Tools;
	
	import data.AppConfig;
	
	import datastructure.RBTree;
	import datastructure.RBTreeNode;
	
	import flash.display.Sprite;
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.core.UIComponent;
	
	import spark.events.IndexChangeEvent;
	
	import view.NodeCircle;
	

	private var nodesArray:Array = new Array(
		new RBTreeNode(8),
		new RBTreeNode(1),
		new RBTreeNode(2),
		new RBTreeNode(7),
		new RBTreeNode(9),
		new RBTreeNode(22),
		new RBTreeNode(6),
		new RBTreeNode(23),
		new RBTreeNode(0),
		new RBTreeNode(45)
	);
	
	private var operation:int = 0;
	private var inputValue:int;
	private var rbt:RBTree;
	private var treeContainer:Sprite;
	private var comp:UIComponent;

	private function init():void
	{
		
		
		//	初始化operCombo
		this.operCombo.dataProvider = new ArrayCollection(AppConfig.OPERATIONS);
		this.operCombo.selectedIndex = 0;
		
		//	初始化树
		rbt = new RBTree();
		rbt.buildRBTree(nodesArray);
		trace("根节点是：" + rbt.root.key + ";高度为：" + rbt.getNodeDistance(rbt.root.left));
		rbt.inorderTraversal();
		treeContainer = new Sprite();
		treeContainer.x = 400;
		treeContainer.y = 30;
		DrawTree.drawNodes(rbt.root,treeContainer);
		DrawTree.drawLines(rbt.root,treeContainer);
		
		comp = new UIComponent();
		comp.addChild(treeContainer);
		treeContainerPanel.addElement(comp);
		
	}

	public function rbtOprationsHandler():void
	{	
		switch (this.operation)
		{
			case AppConfig.SEARCHOPERATION:
				inputValue = int(this.userInput.text);
				var node:RBTreeNode = rbt.search(inputValue);
				if(node.key == -1)
				{
					Alert.show("不存在该节点！","温馨提示");
				}
				else
				{
					Alert.show("您查找的点为：" + node.key + ";颜色为：" + getColorName(node.color) ,"温馨提示");
				}
				break;
			case AppConfig.DELETEOPERATION:
				inputValue = int(this.userInput.text);
				if(rbt.RBTreeDelete(inputValue) == null)
				{
					Alert.show("不存在该节点！","温馨提示");
				}
				else
				{
					refreshTreeDisplay();
				}
				break;
			case AppConfig.INSERTOPERATION:
				inputValue = int(this.userInput.text);
				rbt.insert(new RBTreeNode(inputValue));
				refreshTreeDisplay();
				break;
			case AppConfig.PREDECESSOROPERATION:
				inputValue= int(this.userInput.text);
				if(this.rbt.search(inputValue).key == -1)
				{
					Alert.show("不存在 " +inputValue + " 的前驱！","温馨提示");
				}
				else
				{
					Alert.show(inputValue + " 的前驱是：" + 
						this.rbt.predecessor(this.rbt.search(inputValue)).key,"温馨提示");
				}
				break;
			case AppConfig.SUCCESSOROPERATION:
				inputValue = int(this.userInput.text);
				if(this.rbt.search(inputValue).key == -1)
				{
					Alert.show("不存在 " +inputValue + " 的后继！","温馨提示");
				}
				else
				{
					Alert.show(inputValue + " 的后继是：" + 
						this.rbt.successor(this.rbt.search(inputValue)).key,"温馨提示");
				}
				break;
			default:
				Alert.show("先选择操作哦，亲！","温馨提示");
		}
	}

	
	private function operCombo_changeHandler(evt:IndexChangeEvent):void
	{
		switch (this.operCombo.selectedIndex)
		{
			case 1:
				this.operation = AppConfig.SEARCHOPERATION;
				break;
			case 2:
				this.operation = AppConfig.DELETEOPERATION;
				break;
			case 3:
				this.operation = AppConfig.PREDECESSOROPERATION;
				break;
			case 4:
				this.operation = AppConfig.SUCCESSOROPERATION;
				break;
			case 5:
				this.operation = AppConfig.INSERTOPERATION;
				break;
			default:
				this.operation = AppConfig.NOOPERATION;		
		}
			
	}

	private function onGetMaxNode():void
	{
		Alert.show("最大值是：" + this.rbt.getMaxNode().key,"温馨提示");
		
	}

	private function onGetMinNode():void
	{
		Alert.show("最小值是：" + this.rbt.getMinNode().key,"温馨提示");
	}


	/**
	 * 刷新面板的树显示 
	 * 
	 */	
	private function refreshTreeDisplay():void
	{
		this.rbt.setAllNodesDistance(rbt.root);
		Tools.removeAllChild(this.treeContainer);
		DrawTree.drawNodes(rbt.root,treeContainer);
		DrawTree.drawLines(rbt.root,treeContainer);
	}

	
	private function getColorName(color:int):String
	{
		if(color == 0)
		{
			return "红色";
		}
		else
		{
			return "黑色";
		}
	}









